#include "flash.h"

void fmc_erase(uint32_t start_address,uint32_t page_size,uint32_t page_num)
{
    fmc_unlock();
    fmc_flag_clear(FMC_FLAG_BANK0_END);
    fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
    fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    for(int erase_count = 0; erase_count < page_num; erase_count++)
    {
        fmc_page_erase(start_address + (page_size * erase_count));
        fmc_flag_clear(FMC_FLAG_BANK0_END);
        fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
        fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    }
    fmc_lock();
}

void fmc_write(uint32_t start_address,uint32_t * pdata,uint32_t data_size)
{
    fmc_unlock();
    for(int write_count = 0; write_count < data_size; write_count++)
    {
        fmc_word_program(start_address, *pdata);
        start_address += 4;
        pdata += 1;
        fmc_flag_clear(FMC_FLAG_BANK0_END);
        fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
        fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    }
    fmc_lock();
}
